/*
 * MyButtonSpecialInfo.java
 *
 * Created on May 17, 2000, 12:42 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author ayman
 * In this class we save the special properties of the button "MyButton" which 
 * doesn't exit in the JComponent.
 */
public class MyTextFieldSpecialInfo {
    
    // The info about the button
    private MyTextFieldContainer myTextField;
    private JTextField jtextfield;
    private Vector infoVector = new Vector ();
    private String tableName;
    /************************************************
     *              Vector elements
     * 1- label for text 
     * 2- textField for text
     * 3-label for vertical aligment
     * 4- combobox
     * 5- label for horizontal aligment 
     * 6- combobox
     * 7- label for the icon name 
     * *- panel
     *   8 text for the icon path 
     *   9 button for the file chooser
     *
     ************************************************/
    private JFrame propertiesFrame;
    
    /** Creates a new instance of MyButtonSpecialInfo */
    public MyTextFieldSpecialInfo(MyTextFieldContainer myTextField,JFrame propertiesFrame, String tableName) {
        //System.out.println ("to get button info");
        this.propertiesFrame = propertiesFrame;
        this.myTextField = myTextField ;  
        this.tableName = tableName;
        jtextfield = (JTextField)myTextField.getComponent();
        HandleCompText();
        HandleFont();
        //getInfoVector();
    }
      
    /**
     * This Function is used to retrieve the info specific for the jtextfield
     * By applying some methods used to get those info
     */
    public Vector getInfoVector () {
        return infoVector;
    }
    
    /**
     * This function is used to read the new inforamtion and set it again
     */
    public void getAndSave () {
        String textOnField;
        int typeOfVTxtPos;
        int typeOfHTxtPos;
        String iconFileName;
        int isenable;
        
        
        // get the info of the input
        textOnField =  (String)(((JComboBox)infoVector.elementAt(1)).getSelectedItem());
        //System.out.println ("   dd   " + textOnButton);
        
        //textOnField= tableName+"."+textOnField;
        // setting the info
        jtextfield.setText(textOnField);  
    }
    
    //**************************************************************************
    /**
     *This function get text on the component
     */
    private void HandleCompText () {
        int selectIndex=0;
        Vector fieldsName;
        String txtOnField= jtextfield.getText();
        JLabel compText = new JLabel ("Component text :");
        infoVector.addElement(compText);
        JComboBox tableFields = new JComboBox(); /*comboBox that has all the */
        tableFields.addItem("Unbound");
        //MainWindow.dbCon.setDatabase((String)MainWindow.dbCon.getDatabases().elementAt(0));
         MainWindow.dbCon.setDatabase(MainWindow.selectedDatabase);
        //tableName= (String)MainWindow.dbCon.getTables().elementAt(0);
        //tableName = MyFrame.tableName;
        fieldsName = MainWindow.dbCon.getFields(tableName);
        //MyDB_InfoLoader fields = new MyDB_InfoLoader ();
        //fieldsName = fields.getDB_Info();
        //fieldsName=fields.getDB_Info((String)(fields.getDB_Info((String)(fields.getDB_Info().elementAt(0))).elementAt(0)));
        for (int i=0; i<fieldsName.size() ;i++ ) {
            tableFields.addItem(tableName+"."+(String)fieldsName.elementAt(i));
            if ( txtOnField.equals(tableName+"."+(String)fieldsName.elementAt(i) )) {
                selectIndex=i+1;
            }
        }   
        tableFields.setSelectedIndex(selectIndex);
        infoVector.addElement(tableFields);
        /*JPanel getTableAndFields = new JPanel ();
        JTextField compXTxtField = new JTextField (jtextfield.getText());
        compXTxtField.setEditable(false); // the text became uneditable because 
        // it is used only for getting the table 
        JButton openTableRelation = new JButton ("...");
        openTableRelation.addMouseListener( new MouseAdapter ()  {
            public void mouseClicked(MouseEvent e) {
                MyTableFieldSelector selector = new MyTableFieldSelector ();
            }
        });
        getTableAndFields.add(compXTxtField);
        getTableAndFields.add(openTableRelation);
        //compXTxtField.setText("Table.Field");
        infoVector.addElement(getTableAndFields);*/
   }
    
    

    
   
    
 
     //**************************************************************************
    /**
     * The class implements a click listener to perfomr the file choose window
     * actiom.
     *
     */
    class FileChooserButtonHandler extends MouseAdapter {
        private JTextField iconNameText;
        public FileChooserButtonHandler (JTextField iconNameText) {
            this.iconNameText = iconNameText;
        }
                public void mouseClicked(MouseEvent e) {
                    String fileName;
                    JFileChooser file = new JFileChooser ();
                    file.showSaveDialog(propertiesFrame);
                    fileName = file.getSelectedFile().getPath();
                    iconNameText.setText(fileName);                    
                    //System.out.println (" the file is "+ fileName);
                }
            }
    
 
    
    //**************************************************************************
    /**
     * This function get is enable the button 
     */
    private void HandleFont () {
        JLabel labelFont = new JLabel ("Font Name  :");
        infoVector.add(labelFont);
        JButton openFontChooser = new JButton (" FONT ");
        openFontChooser.addMouseListener(new HandlePressForFont());
        infoVector.add(openFontChooser);
    }
    /**
     * Class for the font selection
     */
    
    class HandlePressForFont extends MouseAdapter {
        
        public void mouseClicked(MouseEvent e) {
            AdvancedFontChooser.getAndSetFont(jtextfield) ;
        }
    }
    
    
   
    
}
